package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/*
    ServletContext对象的创建和销毁的监听器的使用
 */
//@WebListener
public class ServletContextListenerDemo implements ServletContextListener {
    /*
        ServletContext对象创建时执行此方法
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听到了对象的创建...");

        //获取对象
        ServletContext servletContext = sce.getServletContext();
//        System.out.println(servletContext);
        //添加属性
        servletContext.setAttribute("username","zhangsan");
        //替换属性
        servletContext.setAttribute("username","lisi");
        //移除属性
        servletContext.removeAttribute("username");
    }
    /*
        ServletContext对象销毁时执行此方法
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("监听到了对象的销毁...");

    }
}
